//
//  SISSConfirmIntentionBuyerViewController.m
//  SISSAPP
//
//  Created by 袁应荣 on 2019/7/5.
//  Copyright © 2019 优房网. All rights reserved.
//

#import "SISSConfirmIntentionBuyerViewController.h"
#import "SISSImportantHouseTakeLookRecordCell.h"
#import "SISSHouseTakeLookDetailViewController.h"
#import "SISSHouseProgressViewController.h"

#import "SISSNetworkingManager+HouseProgress.h"
#import "SISSNetworkingManager+Progress.h"
#import "SISSHouseTakeLookModel.h"
#import "SISSHouseProgressModel.h"
#import "SISSHouseProgressRecordInfoModel.h"
#import "SISSPassengerCell.h"
#import "SISSPassengerResourceDetailVC.h"

static NSString * const passengerCellID = @"passengerCellID";
@interface SISSConfirmIntentionBuyerViewController ()<SISSImportantHouseTakeLookRecordCellDelegate>

/* 带看模型数组 */
@property (strong, nonatomic) NSArray<SISSHouseTakeLookModel *> *takeLookArr;

@property (strong, nonatomic) NSArray<SISSPassengerResourceModel *> *passengerResourceArr;

@end

@implementation SISSConfirmIntentionBuyerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = self.hasConfirm ? @"交易客户" : @"选择确定购买的客户";
    
    if (self.hasConfirm) {
        // 注册cell
        [self.tableView registerNib:[UINib nibWithNibName:@"SISSPassengerCell" bundle:nil] forCellReuseIdentifier:passengerCellID];
        [self requestTradeBuyerInfoData];
    } else {
        [self requestHouseTakeLookRecordData];
        WeakSelf
        [kSISSRefresh prepareHeaderRefresh:self.tableView loadDataBlock:^{
            kSISSRefresh.refreshType = SISSRefressTypeUpDown;
            [weakSelf upDownLoadData];
        }];
    }
}



- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)confirmButtonDidClickAction {
    [super confirmButtonDidClickAction];
    
    SISSHouseTakeLookModel *takeLookModel = self.takeLookArr[self.selectIndexPath.row];
    [self alertConfirmCacellActionAlertWithTitle:self.hasConfirm ? @"取消交易" : @"确定交易" message:self.hasConfirm ? @"您确定取消双方交易，并放弃后续已经开始的流程" : @"您确定双方已经达成一致购买意见并发起交易吗？" leftConfirmTitle:self.hasConfirm ? @"确定取消交易" : @"确定交易" rightConfirmTitle:@"取消" selectLeftBlock:^{
        if (self.hasConfirm) {
            SISSUploadSuccessViewController *vc = [[SISSUploadSuccessViewController alloc] init];
            vc.title = self.hasConfirm ? @"取消交易" : @"选择交易客户";
            vc.tipStr = self.hasConfirm ? @"交易已取消" : @"交易意向已锁定,请尽快完成合同签署";
            vc.popBackClassType = SISSProgressPopBackClassTypeHouse;
            [self.navigationController pushViewController:vc animated:YES];
        } else {
            NSDictionary *parameters = @{
                                         @"ahbpId" : self.houseProgressModel.ahbpId,
                                         @"adbpId" : takeLookModel.adbpId ? takeLookModel.adbpId : @"-1",
                                         @"abdId" : takeLookModel.abdId,
                                         @"demandType" : @(takeLookModel.takelookType),
                                         };
            [kNetworkManager confirmTradeDemandWithParameters:parameters success:^(id responseObject) {
                SISSUploadSuccessViewController *vc = [[SISSUploadSuccessViewController alloc] init];
                if ([responseObject[@"code"] integerValue] == 200) {
                    vc.title = self.hasConfirm ? @"取消交易" : @"选择交易客户";
                    vc.tipStr = self.hasConfirm ? @"交易已取消" : @"交易意向已锁定,请尽快完成合同签署";
                    vc.popBackClassType = SISSProgressPopBackClassTypeHouse;
                    [self.navigationController pushViewController:vc animated:YES];
                }
                
            } failed:^(NSError *error) {
                NSLog(@"%@",error);
            }];
        }
        
    } selectRightBlock:^{
        
    }];
}


#pragma mark - UITableViewDelegate & UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.hasConfirm ? self.passengerResourceArr.count : self.takeLookArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.hasConfirm) {
        SISSPassengerCell *cell = [tableView dequeueReusableCellWithIdentifier:passengerCellID forIndexPath:indexPath];
        cell.isHideButton = YES;
        cell.passengerModel = self.passengerResourceArr[indexPath.row];
        return cell;
    } else {
        SISSImportantHouseTakeLookRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:importantHouseTakeLookRecordCellReuseID forIndexPath:indexPath];
        cell.delegate = self;
        cell.isHiddenCheckButton = self.hasConfirm;
        cell.takeLookModel = self.takeLookArr[indexPath.row];
        return cell;
    }
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    if (self.hasConfirm) {
        SISSPassengerResourceDetailVC *vc = [[SISSPassengerResourceDetailVC alloc] init];
        vc.title = @"客源详情";
        vc.isHideBottomButton = YES;
        vc.passengerModel = self.passengerResourceArr[indexPath.row];
        [self.navigationController pushViewController:vc animated:YES];
    } else {
        SISSHouseTakeLookDetailViewController *vc = [[SISSHouseTakeLookDetailViewController alloc] init];
        vc.houseProgressModel = self.houseProgressModel;
//        vc.brokerState = self.takeLookArr[indexPath.row].brokerState;
        vc.takeLookModel = self.takeLookArr[indexPath.row];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

#pragma mark -- SISSImportantHouseTakeLookRecordCellDelegate
- (BOOL)importantHouseTakeLookRecordCell:(SISSImportantHouseTakeLookRecordCell *)cell checkButtonIsSelected:(BOOL)isSelected {
    
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    self.takeLookArr[self.selectIndexPath.row].isSelected = NO;
    self.takeLookArr[indexPath.row].isSelected = YES;
    self.selectIndexPath = indexPath;
    [self.tableView reloadData];
    self.isSelectMain = YES;
    
    return NO;
}

#pragma mark - 加载数据
- (void)upDownLoadData {
    
    [self requestHouseTakeLookRecordData];
}

#pragma mark -- 获取房源带看记录数据
- (void)requestHouseTakeLookRecordData {
    
    WeakSelf
    [kNetworkManager requestHouseTakelookRecordWithAhbpId:self.houseProgressModel.ahbpId success:^(NSArray<SISSHouseTakeLookModel *> *takeLookArr) {
        
        weakSelf.takeLookArr = takeLookArr;
        
        [weakSelf.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
            if (takeLookArr.count * 110 > SCREEN_HEIGHT - kTopHeight - 120) {
                make.height.mas_equalTo(SCREEN_HEIGHT - kTopHeight - 120);
            } else {
                make.height.mas_equalTo(takeLookArr.count * 110);
            }
        }];
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            [weakSelf.tableView reloadData];
            [weakSelf.tableView.mj_header endRefreshing];
        }];
        
    } failed:^(NSError *error) {
        
        [weakSelf.tableView.mj_header endRefreshing];
        NSLog(@"%@",error);
    }];
}

- (void)requestTradeBuyerInfoData {
    WeakSelf
    [kNetworkManager requestTradeBuyerInfoWithAhbpId:self.houseProgressModel.ahbpId success:^(SISSPassengerResourceModel *buerInfoModel) {
        
        weakSelf.passengerResourceArr = @[buerInfoModel];
        
        [weakSelf.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(1 * 66);
        }];
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            [weakSelf.tableView reloadData];
            [weakSelf.tableView.mj_header endRefreshing];
        }];
    } failed:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}

#pragma mark - Getter & Setter

- (NSArray<SISSPassengerResourceModel *> *)passengerResourceArr {
    if (!_passengerResourceArr) {
        _passengerResourceArr = [NSArray array];
    }
    return _passengerResourceArr;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
